//
//  NSDate+WJExtension.m
//  每日一笑
//
//  Created by WJ on 16/2/28.
//  Copyright © 2016年 WJ. All rights reserved.
//

#import "NSDate+WJExtension.h"

@implementation NSDate (WJExtension)

/**
 *  比较from 和self 的时间差值
 */
- (NSDateComponents *) deltaFrom:(NSDate *)from {
    
    // 日历
    NSCalendar *calender = [NSCalendar currentCalendar];
    
    // 比较时间
    NSCalendarUnit unit = NSCalendarUnitDay | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    return [calender components:unit fromDate:from toDate:self options:0];
    
}

/**
 *  判断是否是今年
 */
- (BOOL)isThisYear {
    
    // 日历
    NSCalendar *calender = [NSCalendar currentCalendar];
    
    NSInteger nowYear = [calender component:NSCalendarUnitYear fromDate:[NSDate date]];
    NSInteger selfYear = [calender component:NSCalendarUnitYear fromDate:self];
    
    return nowYear == selfYear;
    
}

/**
 *  判断是否是今天
 */
- (BOOL)isToday {
    
    // 日期格式化类
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    
    NSString *nowString = [formatter stringFromDate:[NSDate date]];
    NSString *selfString = [formatter stringFromDate:self];
    
    // 判断当前日期 和 传过来的日期是否是同一天
    return nowString == selfString;
    
}

/**
 *  判断是否是昨天
 */
- (BOOL)isYesterDay {
    
    // 日期格式化类 把当前时间转化成 固定的格式
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    /**[formatter stringFromDate:[NSDate date]] 把当前的时间格式转化成上面只有 年-月-日的 时间字符串
     * formatter dateFromString: 再把上面的固定时间字符串改成 date 类型
     */
    NSDate *nowDate = [formatter dateFromString:[formatter stringFromDate:[NSDate date]]];
    NSDate *selfDate = [formatter dateFromString:[formatter stringFromDate:self]];
    
    // 日历
    NSCalendar  *calender = [NSCalendar currentCalendar];
    
    NSDateComponents *components = [calender components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:selfDate toDate:nowDate options:0];
    
    //
    return components.year == 0 && components.month == 0 && components.day == 1;
    
}
@end
